package com.blockchain.securewallet.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.WalletApp;
import com.blockchain.securewallet.base.BaseActivity;
import com.blockchain.securewallet.model.db.CoinWallet;
import com.blockchain.securewallet.utils.ShowPopUtil;
import com.blockchain.securewallet.utils.ToastUtils;
import com.blockchain.securewallet.utils.ToolUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

public class ReceiveCoinActivity extends BaseActivity {

    public static final String TAG = "Wallet.Receive";

    public static final String KEY_WALLET_ID = "wallet_id";

    @BindView(R.id.tv_title)
    TextView textView;

    @BindView(R.id.tv_address)
    TextView tvAddress;

    @BindView(R.id.tv_unit)
    TextView tvUnit;

    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;

    @BindView(R.id.tv_value)
    TextView tvValue;

    private CoinWallet wallet;
    private ShowPopUtil showPopUtil;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_coin);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent != null) {
            int id = intent.getIntExtra(KEY_WALLET_ID, 0);
            wallet = WalletApp.getInstance().queryCurrrentWallet().get(id);
//            wallet = WalletApp.getInstance().getDaoSession().load(CoinWallet.class, id);
            if (wallet.getType() == CoinWallet.TYPE_BIT_COIN) {
                textView.setText(R.string.bit_coin_name);
                tvUnit.setText(R.string.bit_coin_unit);
            } else if (wallet.getType() == CoinWallet.TYPE_ETH_COIN){
                textView.setText(R.string.eth_coin_name);
                tvUnit.setText(R.string.eth_coin_unit);
            } else if (wallet.getType() == CoinWallet.TYPE_LTC_COIN){
                textView.setText(R.string.ltc_coin_name);
                tvUnit.setText(R.string.ltc_coin_unit);
            } else if (wallet.getType() == CoinWallet.TYPE_BCH_COIN){
                textView.setText(R.string.bch_coin_name);
                tvUnit.setText(R.string.bch_coin_unit);
            }
            tvAddress.setText(wallet.getAddress());
            generateQrCode();
        } else {
            textView.setText(R.string.wallet_receive);
        }
    }

    @OnClick(R.id.btn_back)
    void back() {
        finish();
    }

    private void generateQrCode() {
        String address = wallet.getAddress();
//        String valueStr = tvValue.getText().toString();
//        JSONObject root = new JSONObject();
//        long value;
//        if(wallet.getType()==CoinWallet.TYPE_BIT_COIN) {
//            //value = (long)(Double.valueOf(valueStr)*(10^8));
//            value = BigDecimal.valueOf(Double.parseDouble(valueStr)).multiply(BigDecimal.valueOf(100_000_000)).longValueExact();
//        }else {
//            value = Convert.toWei(valueStr, Convert.Unit.ETHER).longValue();
//        }
//        try {
//            root.put("a",address);
//            root.put("v", value);
//            root.put("r", 1);
//            root.put("t", wallet.getType());
//        }catch (Exception e){
//            return ;
//        }


        if (Double.parseDouble(tvValue.getText().toString()) == 0) {
            content = address;
        } else {
            content = address + ":" + tvValue.getText().toString();
        }


        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                return QRCodeEncoder.syncEncodeQRCode(content, BGAQRCodeUtil.dp2px(ReceiveCoinActivity.this, 150));
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    ivQrCode.setImageBitmap(bitmap);
                    ivQrCode.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(ReceiveCoinActivity.this, "生成二维码失败", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }

    @OnClick(R.id.btn_set_value)
    void setValue() {

        showPopUtil = new ShowPopUtil(R.layout.pw_set_receive_money, ReceiveCoinActivity.this, 0.7, 0.3) {
            @Override
            protected void initView(View view) {

                final EditText et_money = view.findViewById(R.id.et_money);
                TextView tv_cancel = view.findViewById(R.id.tv_cancel);
                TextView tv_comfirm = view.findViewById(R.id.tv_comfirm);

                tv_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showPopUtil.dismiss();
                    }
                });
                tv_comfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String money = et_money.getText().toString();
                        if (money.equals("")) {
                            ToastUtils.showToast("请输入接收金额");
                        } else {
                            //最后1为是小数点就特殊处理
                            if (ToolUtils.lastPointJudge(money)) {
                                ToastUtils.showToast("最后1位不能为小数点");
                                return;
                            }

                            tvValue.setText(money);
                            showPopUtil.dismiss();
                            generateQrCode();
                        }
                    }
                });
            }
        };
        showPopUtil.showPop();

//        String titleInsert  = "收款金额";
//        DialogFragmentHelper.showInsertDialog(getSupportFragmentManager(), titleInsert, new IDialogResultListener<String>() {
//            @Override
//            public void onDataResult(String result) {
////                Toast.makeText(ReceiveCoinActivity.this,result,Toast.LENGTH_SHORT).show();
//                Log.i("testtest",result+"无语");
//                tvValue.setText(result);
////                generateQrCode();
//            }
//        }, true);
    }
}
